package com.wjy.rbac.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjy.rbac.entity.Role;
import com.wjy.rbac.entity.RolePower;
import com.wjy.rbac.mapper.RoleMapper;
import com.wjy.rbac.service.IPowerService;
import com.wjy.rbac.service.IRolePowerService;
import com.wjy.rbac.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ybxxszl
 * @since 2019-04-12
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    @Qualifier(value = "rolePowerServiceImpl")
    private IRolePowerService rolePowerService;

    @Autowired
    @Qualifier(value = "powerServiceImpl")
    private IPowerService powerService;

    @Override
    public void insert(Role role) {
        role.setStatus(0);
        save(role);
    }

    @Override
    public void update(Role role) {
        role.setStatus(0);
        updateById(role);
    }

    @Override
    public void delete(String roleId) {
        removeById(roleId);
    }

    @Override
    public Map<String, Object> get(String roleId) {
        Map<String, Object> result = new HashMap<String, Object>();
        Role role = getById(roleId);
        result.put("role", role);
        List<RolePower> rolePowerList = rolePowerService.getByRoleId(roleId);
        List<String> powerIdList = new ArrayList<String>();
        for (RolePower rolePower : rolePowerList) {
            powerIdList.add(rolePower.getPowerId());
        }
        List<Map<String, Object>> powerList = powerService.getByPowerIdList(powerIdList);
        result.put("powerList", powerList);
        return result;
    }

    @Override
    public List<Map<String, Object>> getByRoleIdList(List<String> roleIdList) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for (String roleId : roleIdList) {
            result.add(get(roleId));
        }
        return result;
    }

}
